package com.yixiu.db.druid;

import org.apache.commons.dbutils.QueryRunner;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

/**
 * Created by jinhe on 2019/3/15.
 */
public class DruidUtils {

    private static Connection connection = null;
    //获取元数据
    public static DataSource getDatasource() {
        DataSource dataSource = DruidConnection.getInstace().getDataSource();
        return dataSource;
    }

    //获取链接
    public static Connection getConnection() {
        connection = DruidConnection.getInstace().getConnection();
        return connection;
    }

    //归还资源
    public void release() {
        try {
            if(connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 测试代码如下
     * @param args
     * @throws SQLException
     */
    public static void main(String[] args) throws SQLException {
        //编写sql语句
        String sql ="select *from user";

        //获取链接
        QueryRunner runner = new QueryRunner(DruidUtils.getDatasource());
        //返回查询值
//        List<User> user = runner.query(sql,new BeanListHandler<>(User.class));

//        System.out.println(user);
    }



}

